package com.zhyt.intelligentpipeline.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhyt.intelligentpipeline.exception.CustomException;
import com.zhyt.intelligentpipeline.exception.ErrorBody;
import com.zhyt.intelligentpipeline.mapper.CorrosionDetectionMapper;
import com.zhyt.intelligentpipeline.mapper.PipelineMapper;
import com.zhyt.intelligentpipeline.model.CorrosionDetection;
import com.zhyt.intelligentpipeline.model.Pipeline;
import com.zhyt.intelligentpipeline.service.CorrosionDetectionService;
import com.zhyt.intelligentpipeline.service.PipelineService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CorrosionDetectionServiceImpl extends ServiceImpl<CorrosionDetectionMapper, CorrosionDetection>implements CorrosionDetectionService {

    @Resource
    private PipelineService pipelineService;

    @Resource
    private CorrosionDetectionMapper corrosionDetectionMapper;

    @Override
    public List<CorrosionDetection> getCorrosionDetectionByPipelineID(String pipelineId) {
        //通过管线ID获取起点站和终点站的名称
        Pipeline pipeline = pipelineService.getPipelineById(pipelineId);
        if(pipeline == null){
            throw CustomException.of(HttpStatus.BAD_REQUEST,new ErrorBody(550,"没有这条管线的信息"));
        }
        String initStationName = pipeline.getInitialStation();
        String endStationName = pipeline.getEndStation();
        //获取该站点的所有腐蚀缺陷
        LambdaQueryWrapper<CorrosionDetection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CorrosionDetection::getEndStation,endStationName).eq(CorrosionDetection::getInitialStation,initStationName);
        List<CorrosionDetection> corrosionDetections = corrosionDetectionMapper.selectList(queryWrapper);
        //将腐蚀缺陷按照里程排序
        return corrosionDetections.stream().sorted(Comparator.comparing(CorrosionDetection::getMileage)).collect(Collectors.toList());
    }

    @Override
    public int getCorrosionDetectionsByCount(String pipelineId) {
        Pipeline pipeline = pipelineService.getPipelineById(pipelineId);
        if(pipeline == null){
            throw CustomException.of(HttpStatus.BAD_REQUEST,new ErrorBody(550,"没有这条管线的信息"));
        }
        String initStationName = pipeline.getInitialStation();
        String endStationName = pipeline.getEndStation();
        //获取该站点的所有腐蚀缺陷
        LambdaQueryWrapper<CorrosionDetection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CorrosionDetection::getEndStation,endStationName).eq(CorrosionDetection::getInitialStation,initStationName);
        return corrosionDetectionMapper.selectList(queryWrapper).size();
    }

    public static void main(String[] args) {
        List<Integer> values = Arrays.asList(1,26,7,2,8,3,0,-1,-3);
        values.stream().sorted().forEach(item->{
            System.out.print(item +" ");
        });

    }
}
